// pages/user/myaddress.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenLoading: false,
    dateList: [],
    userInfo: {},
    route: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let pages = getCurrentPages();
    let prevpage = pages[pages.length - 2];
    this.setData({
      route: prevpage.route
    })
    console.log(prevpage.route, 'prevpage')
  },

  //下拉刷新
  onPullDownRefresh: function () {
    this.getAddress();
    wx.stopPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    //用户授权登录
    app.login(function (userInfo) {
      that.setData({
        userInfo: userInfo
      });
      that.getAddress();
    });
  },
  goEdit(e) {

  },
  /**
   * 获取地址列表
   */
  getAddress: function () {
    var that = this;
    var paraArr = new Array();
    paraArr['user_id'] = that.data.userInfo.userID;
    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api_address',
      data: {
        user_id: paraArr['user_id'],
        sign: sign
      },
      method: 'GET',
      header: {},
      success: function (res) {
        console.log(res.data);
        that.setData({
          dateList: res.data,
          hiddenLoading: true
        });
      }
    })
  },
  getWeixinAddress: function () {
    var that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.address']) {
          //已经允许过
          wx.chooseAddress({
            success(res) {
              console.log(1)
              that.formSubmit(res.provinceName, res.cityName, res.countyName,res.detailInfo,res.userName,res.telNumber)
              console.log(2)
              that.setData({
                region: [res.provinceName, res.cityName, res.countyName],
                province: res.provinceName,
                city: res.cityName,
                county: res.countyName,
                address: res.detailInfo,
                address1: res.detailInfo,
                name: res.userName,
                tel: res.telNumber,
                latitude: '',
                longitude: '',
              })
            }
          })

        } else {
          if (res.authSetting['scope.address'] == false) {
            //如果之前拒绝了就调起设置窗口
            wx.openSetting({
              success(res) {
                console.log(res.authSetting)
              }
            })
          } else {
            //首次允许后
            wx.chooseAddress({
              success(res) {
                that.formSubmit(res.provinceName, res.cityName, res.countyName,res.detailInfo,res.userName,res.telNumber)
                that.setData({
                  region: [res.provinceName, res.cityName, res.countyName],
                  province: res.provinceName,
                  city: res.cityName,
                  county: res.countyName,
                  address: res.detailInfo,
                  address1: res.detailInfo,
                  name: res.userName,
                  tel: res.telNumber,
                  latitude: '',
                  longitude: '',
                })
              }
            })
          }
        }
      }
    })
  },
  formSubmit: function (province,city,county,address,name,tel) {
    console.log(3)
    console.log(province,city,county,address,name,tel,'获取到的信息')
    console.log(4)

    var that = this;
    var m_province = province;
    var m_city = city;
    var m_county = county;
    var m_addr = address;
    var m_name = name;
    var m_tel = tel;
    //return
    wx.request({
      url: rootDocment + '/api_address',
      data: {
        m_province: m_province,
        m_city: m_city,
        m_county: m_county,
        m_address: m_addr,
        m_name: m_name,
        m_tel: m_tel,
        m_latitude: '',
        m_longitude:'',
        m_address1: m_addr,
        m_default: 0,
        m_label: 0,
        user_id: that.data.userInfo.userID
      },
      method: 'POST',
      header: {},
      success: function (res) {
        console.log(res)
        if(res.data.code=='1001'){
          wx.showToast({
            title: '添加成功',
            icon:'none'
          })
          that.getAddress();
        }else{
          wx.showToast({
            title: res.msg,
            icon:'none'
          })
        }
        // wx.navigateBack({
        //   delta: 1
        // })
      }
    })
  },
  /**
   * 设置默认地址
   */
  changeAddress: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var key = e.currentTarget.dataset.key;
    var dateList = that.data.dateList;
    for (var j = 0; j < dateList.length; j++) {
      dateList[j]['is_default'] = 0;
    }
    dateList[key]['is_default'] = 1;
    that.setData({
      dateList: dateList
    });
    wx.request({
      url: rootDocment + '/api_address/' + id,
      data: {
        m_default: 1,
        user_id: that.data.userInfo.userID
      },
      method: 'PUT',
      header: {},
      success: function (res) {
        if (that.data.route == 'pages/order/index') {
          wx.navigateBack({
            delta: 1
          })
        }
        if (that.data.route == 'pages/user/vip') {
          wx.navigateBack({
            delta: 1
          })
        }

      }
    })
  },

  /**
   * 添加地址
   */
  addAddress: function () {
    app.redirect('user/addaddress');
  },

  /**
   * 编辑地址
   */
  editAddress: function (e) {
    var id = e.currentTarget.dataset.id
    if (!id) return;
    app.redirect('user/addressAddorEdit', 'id=' + id);
  },

})